package com.pkche.core.advanced.reprint.extract;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.pkche.core.advanced.reprint.NetArticle;
import com.pkche.core.advanced.reprint.NetArticle.ContextType;
import com.pkche.core.advanced.reprint.NetArticle.TargetType;

/**
 * 微信朋友圈文章提取器<br/>
 * Describe：提取微信朋友圈文章<br />
 * User："Qiang" <br/>
 * Time：2015年2月13日 下午4:14:00 <br/>
 */
public class WeChatExtractor extends ArticleExtractor {

	public WeChatExtractor(URL url, int millis) throws IOException {
		super(url, millis);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 默认的空构造，只允许子类调用
	 */
	protected WeChatExtractor() {
	}

	@Override
	public Element contextElement() {
		// TODO Auto-generated method stub
		return document.select("div#js_content").first();
	}

	@Override
	public String title() {
		Element titElement = document.select("h2#activity-name").first();
		if (titElement == null) {
			// 提取失败
			return null;
		}
		return titElement.text();
	}

	@Override
	public TargetType targetType() {
		// TODO Auto-generated method stub
		return TargetType.wechat;
	}

	@Override
	public boolean isIncludeMedia() {
		if (contextElement == null) {
			return false;
		}
		Element media = contextElement.select("iframe.video_iframe").first();
		return media == null ? false : true;
	}

	@Override
	public boolean isIncludeAudio() {
		// TODO Auto-generated method stub
		return false;
	}

	// 微信文章页面存在页眉缩略图 <div class="rich_media_thumb_wrp">，需重写
	@Override
	public NetArticle get() {
		NetArticle ar = super.get();
		// 由于微信使用JS载入的第一张图，所以不能够使用Jsoup进行分析，需要额外判断
		Element wrp = document.select("div.rich_media_thumb_wrp").first();
		if (wrp != null) {
			String html = wrp.html();
			String lab = "var cover = \"";
			// 从js变量中获取图像路径
			int begin = html.indexOf(lab);
			if (begin == -1) {
				return ar;
			}
			html = html.substring(begin + lab.length(), html.length());
			int end = html.indexOf("\"");
			if (end == -1) {
				return ar;
			}
			String src = html.substring(0, end);
			try {
				// 设置第一张缩略图
				ar.setFristImage(new URL(src));
				// 重新设置类型
				ar.setContextType(ContextType.image_text);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
		return ar;

	}

	// 微信使用data-src，需重写
	public List<URL> imageList() {
		// <img src>
		// System.out.println(contextElement.html());
		List<URL> imgList = new ArrayList<URL>();
		if(contextElement == null) return imgList;
		Elements imgs = contextElement.select("img");
		// 遍历页面图像元素
		for (Element e : imgs) {
			String src = e.attr("data-src");
			if ((src == null) || (src.trim().length() == 0)) {
				src = e.attr("src");
				if (src == null) {
					continue;
				}
			}
			// System.out.println(">>>>>>>>> img " + src);
			try {
				URL resource = new URL(src);
				imgList.add(resource);
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				// e1.printStackTrace();
				// 不合法的URL丢弃
			}
		}
		return imgList;
	}

}
